package com.yun.algorithmproblem.huaweiod;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class OD362 {

    static class Student {
        String subject;
        int[] score;

        public Student(String subject, int m) {
            this.subject = subject;
            score = new int[m + 1];
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        String[] subjects = new String[m];
        for (int i = 0; i < m; i++) {
            subjects[i] = in.next();
        }
        Student[] students = new Student[n];
        for (int i = 0; i < n; i++) {
            int total = 0;
            String subject = in.next();
            students[i] = new Student(subject, m);
            for (int j = 1; j <= m; j++) {
                int score = in.nextInt();
                total += score;
                students[i].score[j] = score;
            }
            students[i].score[0] = total;
        }
        String targetSubject = in.next();
        int targetNum = 0;
        for (int i = 0; i < m; i++) {
            if (subjects[i].equals(targetSubject)) {
                targetNum = i + 1;
                break;
            }
        }
        int finalTargetNum = targetNum;
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int x = o2.score[finalTargetNum] - o1.score[finalTargetNum];
                return x == 0 ? o1.subject.compareTo(o2.subject) : x;
            }
        });
        for (Student s : students) {
            System.out.print(s.subject + " ");
        }
    }
}
